using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
using KMS.Core.application;
using KMS.Web.Views.News;
using KMS.Core.domain.model.alert;

namespace KMS.Web.Controllers
{
    public class NewsController : Controller
    {
        private INewsService _newsService;
        private int _displayedChars;

        //
        // GET: /News/

        public ActionResult List()
        {
            return View(_newsService.FindNewsForCurrentUser()
                .Select(e => CreateViewModelFromModel(e)).ToList());
        }

        private AlertViewModel CreateViewModelFromModel(Alert e)
        {
            if (e is SystemAlert)
            {
                return new SystemAlertViewModel(e as SystemAlert, _displayedChars);
            }
            else if (e is PostAlert)
            {
                return new ResourceAlertViewModel(e as PostAlert);
            }
            else throw new ArgumentException(String.Format("Type {0} is not supported", e.GetType()));
        }

        public NewsController(INewsService newsService, int displayedChars)
        {
            _newsService = newsService;
            _displayedChars = displayedChars;
        }
    }
}
